<template>
  <el-dialog width="60%" top="10vh" :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="地区名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入地区名称"></el-input>
      </el-form-item>
      <el-form-item label="上级地区" prop="parentId">
        <el-select v-model="dataForm.parentId" placeholder="请输入地区">
          <el-option v-for="item in selectRegionList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="地区级别" prop="level">
        <el-select v-model="dataForm.level" clearable placeholder="请选择地区级别">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="dataFormSubmit()">确定</el-button>
      <el-button size="small" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRegionInfo, createRegion, updateRegion, getSelectRegionList } from '@/api/sys/region';

const defaultDataForm = {
  id: '',
  name: '',
  parentId: '0',
  level: '',
}

export default {
  data() {
    return {
      options: [{
        value: '1',
        label: '省份'
      }, {
        value: '2',
        label: '城市'
      }, {
        value: '3',
        label: '地区'
      }],
      selectRegionList: [],
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      dataRule: {
        name: [
          { required: true, message: '地区名称不能为空', trigger: 'blur' }
        ],
        parentId: [
          { required: true, message: '上级地区不能为空', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '地区级别不能为空', trigger: 'blur' }
        ],
      },
      preId: '',
      preName: '',
    }
  },
  methods: {
    init(id, preLevel, preId, preName) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getRegionInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.name = res.data.name;
              this.dataForm.parentId = res.data.parentId;
              this.dataForm.level = res.data.level;
            }
          })
        }
      })
      if(preLevel === 1) {
        this.selectRegionList = [];
        this.selectRegionList.push({ id: '0', name: '无上级菜单' });
      } else {
        this.selectRegionList = [];
        this.selectRegionList.push({ id: preId, name: preName });
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            name: this.dataForm.name,
            parentId: this.dataForm.parentId,
            level: this.dataForm.level,
          }
          if (!this.dataForm.id) {
            // 创建
            createRegion(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            // 更新
            updateRegion(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
  }
}
</script>
